# -*- coding: utf-8 -*-

"""
调试Selenium爬虫
"""

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.chrome.service import Service
import time

def test_selenium():
    """
    测试Selenium基本功能
    """
    print("开始测试Selenium基本功能...")
    
    try:
        # 配置Chrome选项
        chrome_options = Options()
        chrome_options.add_argument("--headless")  # 无头模式
        chrome_options.add_argument("--no-sandbox")
        chrome_options.add_argument("--disable-dev-shm-usage")
        chrome_options.add_argument("--disable-gpu")
        chrome_options.add_argument("--window-size=1920,1080")
        chrome_options.add_argument("user-agent=Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36")
        
        # 指定chromedriver路径
        service = Service("/usr/bin/chromedriver")
        
        # 创建WebDriver实例
        print("正在初始化WebDriver...")
        driver = webdriver.Chrome(service=service, options=chrome_options)
        
        # 访问Amazon热销榜首页
        print("正在访问Amazon热销榜首页...")
        driver.get("https://www.amazon.com/Best-Sellers/zgbs")
        time.sleep(5)  # 等待页面加载
        
        # 检查页面标题
        print(f"页面标题: {driver.title}")
        
        # 尝试不同的选择器查找商品元素
        selectors = [
            "div.zg-grid-general-faceout",
            "div[data-testid='grid-deals-container'] div[data-testid='deal-card']",
            "div[data-component-type='s-search-result']",
            "div.sg-col-4-of-24",
            "div.zg-grid-general-faceout a[href*='/dp/']",
            "a[href*='/dp/']",
            "div.zg-carousel-content div.a-section",
            "div.zg-item-immersion",
            "div.zg-item",
        ]
        
        print("正在尝试不同的选择器查找商品元素...")
        for selector in selectors:
            try:
                product_elements = driver.find_elements(By.CSS_SELECTOR, selector)
                print(f"选择器 '{selector}' 找到 {len(product_elements)} 个元素")
                if len(product_elements) > 0:
                    # 显示前几个元素的文本
                    for i, element in enumerate(product_elements[:3]):
                        text = element.text.strip()
                        if text:
                            print(f"  元素 {i+1}: {text[:100]}...")
                        else:
                            print(f"  元素 {i+1}: (空文本)")
            except Exception as e:
                print(f"使用选择器 '{selector}' 时出错: {e}")
        
        # 获取页面源码用于分析
        print("\n页面源码前1000字符:")
        print(driver.page_source[:1000])
        
        # 关闭浏览器
        driver.quit()
        print("测试完成!")
        
    except Exception as e:
        print(f"测试过程中出现错误: {e}")
        import traceback
        traceback.print_exc()

if __name__ == "__main__":
    test_selenium()